package Wuzi;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.DataOutputStream;
import java.net.Socket;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import static Wuzi.MusicPlay.Stop_music;
import static Wuzi.MusicPlay.play_Music;

//游戏窗口类

public class Wframe extends JFrame  {
    //因为要添加事件，所以要声明为全局
    private boolean playmusic=false;
    private JCheckBox jcb;
    private JButton btn1;
    private JButton btn2;
    private JButton btn3;//求助按钮
    private JRadioButton jr1;//模式
    private JRadioButton jr3;//智能
    private JComboBox<Integer>jc1;//深度
    private JComboBox<Integer>jc2;//节点
    private JRadioButton jr5;//先手
    private JTextArea jt;
    private JCheckBox jmusic;
    private JCheckBox jaudio;
    private JComboBox<Integer>type;
    WPanel jp=new WPanel();

    private static final String HOST="127.0.0.1";//ip
    private static final int POST=8888;//端口

    private Socket socket=null;
    private DataOutputStream dos=null;


    public void start() {
        add(jp,BorderLayout.WEST);
        JPanel rp=new JPanel();
        rp.setPreferredSize(new Dimension(230, 700));
        rp.setLayout(new BoxLayout (rp,BoxLayout.Y_AXIS));

        //文本框
        JPanel panel1=new JPanel();
        panel1.setLayout(new BorderLayout());
        panel1.setBorder(new TitledBorder("单击鼠标右键，查看信息") );
        jt=new JTextArea();
        jt.setEditable(false);
        JScrollPane jScrollPane=new JScrollPane();
        jScrollPane.setViewportView(jt);
        panel1.add(jScrollPane);
        rp.add(panel1);
        add(rp,BorderLayout.EAST);

        //声音
        JPanel panelx=new JPanel();
        panelx.setBorder(new TitledBorder("声音"));
        JLabel jb0=new JLabel("音乐");
        jmusic=new JCheckBox();
        JLabel jbs=new JLabel("音效");
        jaudio=new JCheckBox();
        panelx.add(jb0);
        panelx.add(jmusic);
        panelx.add(jbs);
        panelx.add(jaudio);
        rp.add(panelx);

      //拓展
        JPanel panel0=new JPanel();
        panel0.setBorder(new TitledBorder("拓展"));
        JLabel jbx=new JLabel("棋型");
        type=new JComboBox<>(new Integer[] {0,1,2});
        btn3=new JButton("援助");
        panel0.add(jbx);
        panel0.add(type);
        panel0.add(btn3);
        rp.add(panel0);

        //模式
        JPanel panel2=new JPanel();
        panel2.setBorder(new TitledBorder("模式"));
        jr1=new JRadioButton("人人对战");
        jr1.setSelected(true);
        JRadioButton jr2=new JRadioButton("人机对战");
        //归为一组，结果互斥
        ButtonGroup bg1=new ButtonGroup();
        bg1.add(jr1);
        bg1.add(jr2);
        panel2.add(jr1);
        panel2.add(jr2);
        rp.add(panel2);

        //智能
        JPanel panel3=new JPanel();
        panel2.setBorder(new TitledBorder("智能"));
        jr3=new JRadioButton("估值函数");
        jr3.setSelected(true);
        JRadioButton jr4=new JRadioButton("估值函数+搜素树");
        //归为一组，结果互斥
        ButtonGroup bg2=new ButtonGroup();
        bg2.add(jr3);
        bg2.add(jr4);
        panel3.add(jr3);
        panel3.add(jr4);
        rp.add(panel3);

        //模式
        JPanel panel4=new JPanel();
        panel3.setBorder(new TitledBorder("搜索树"));
        JLabel jb1=new JLabel("搜索深度");
        jc1=new JComboBox<>(new Integer[] {1,2,3});
        JLabel jb2=new JLabel("每层节点");
        jc2=new JComboBox<>(new Integer[] {1,2,3});
        panel4.add(jb1);
        panel4.add(jc1);
        panel4.add(jb2);
        panel4.add(jc2);
        rp.add(panel4);

        //其他
        JPanel panel5=new JPanel();
        panel5.setBorder(new TitledBorder("其他"));
        JLabel jb3=new JLabel("显示顺序");
        jcb=new JCheckBox();
        btn1=new JButton("悔棋");
        btn2=new JButton("新游戏");

        jcb.addMouseListener(mouseListener);
        btn1.addMouseListener(mouseListener);
        btn2.addMouseListener(mouseListener);
        jmusic.addMouseListener(mouseListener);
        jaudio.addMouseListener(mouseListener);
        btn3.addMouseListener(mouseListener);

        panel5.add(jb3);
        panel5.add(jcb);
        panel5.add(btn1);
        panel5.add(btn2);
        rp.add(panel5);

        //人机模式
        JPanel panel6=new JPanel();
        panel6.setBorder(new TitledBorder("人机模式"));
        jr5=new JRadioButton("人类先手");
        jr5.setSelected(true);
        JRadioButton jr6=new JRadioButton("电脑先手");
        //归为一组，结果互斥
        ButtonGroup bg3=new ButtonGroup();
        bg3.add(jr5);
        bg3.add(jr6);
        panel6.add(jr5);
        panel6.add(jr6);
        rp.add(panel6);

        //主界面
        setSize(Wfinal.GAME_WIDTH,Wfinal.GAME_HEIGHT);
        setResizable(false);
        setLocation(200,20);
        setTitle("五子棋");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    MouseListener mouseListener=new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            Object obj=e.getSource();
            if(obj==jcb) {

                jp.showOder(jcb.isSelected());


            }else if(obj==btn1) {
                //悔棋
                jp.huiQi();
            }else if(obj==btn2) {
                //新游戏
                jp.newGame(jr1.isSelected(), jr3.isSelected(),(int)jc1.getSelectedItem(),
                        (int)jc2.getSelectedItem(), jr5.isSelected(),jcb.isSelected(), jt,(int)type.getSelectedItem());
                jcb.setEnabled((int)type.getSelectedItem()==0?true:false);


            }else if(obj==jmusic){
              if(jmusic.isSelected())
              {
                  play_Music();
              }
              else{
                  Stop_music();
              }

            }else if(obj==btn3){
               jp.isHelp(true);
            } else if(obj==jaudio){

            jp.playAudio(jaudio.isSelected());
        }

        }
    };



    public static void main(String[] args) {
        Wframe wf=new Wframe();
        wf.start();
    }
}
